package com.itany.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.DictConstant;
import com.itany.cms.constant.StatusConstant;
import com.itany.cms.dao.CommentMapper;
import com.itany.cms.dao.UserMapper;
import com.itany.cms.entity.*;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.CommentService;
import com.itany.cms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Comment> findByParams(Comment commentParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COMMENT_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COMMENT_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!ParameterUtil.isNull(commentParam.getUserName())) {
            wrapper.eq("nickname", commentParam.getUserName());
        }
        List<User> users = userMapper.selectList(wrapper);
        QueryWrapper<Comment> wrapper1 = new QueryWrapper<>();
        if (!ParameterUtil.isNull(commentParam.getUserName())) {
            for (User user : users) {
                wrapper1.eq("user_id", user.getId());
            }
        }
        if (!ParameterUtil.isNull(commentParam.getContext())) {
            wrapper1.like("context", commentParam.getContext());
        }
        if (!ParameterUtil.isNull(commentParam.getStartDate()) || !ParameterUtil.isNull(commentParam.getEndDate())) {
            wrapper1.between("createDate", commentParam.getStartDate(),commentParam.getEndDate());
        }
        List<Comment> comments = commentMapper.selectList(wrapper1);
        for (Comment comment:comments) {
            QueryWrapper<User> wrapper2 = new QueryWrapper();
            wrapper1.eq("id",comment.getUserId());
            List<User> users1 = userMapper.selectList(wrapper2);
            comment.setUserName(users1.get(0).getNickname());
        }
        PageInfo<Comment> commentPage = new PageInfo<>(comments);
        return commentPage;
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setStatus(Integer.parseInt(status));
        commentMapper.updateById(comment);
    }
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Comment> findByParams2(Comment commentParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COMMENT_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COMMENT_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!ParameterUtil.isNull(commentParam.getUserName())) {
            wrapper.eq("nickname", commentParam.getUserName());
        }
        List<User> users = userMapper.selectList(wrapper);
        QueryWrapper<Comment> wrapper1 = new QueryWrapper<>();
        if (!ParameterUtil.isNull(commentParam.getUserName())) {
            for (User user : users) {
                wrapper1.eq("user_id", user.getId());
            }
        }
        if (!ParameterUtil.isNull(commentParam.getContext())) {
            wrapper1.like("context", commentParam.getContext());
        }
        if (!ParameterUtil.isNull(commentParam.getStartDate()) || !ParameterUtil.isNull(commentParam.getEndDate())) {
            wrapper1.between("createDate", commentParam.getStartDate(),commentParam.getEndDate());
        }
        List<Comment> comments = commentMapper.selectList(wrapper1);
        for (Comment comment:comments) {
            QueryWrapper<User> wrapper2 = new QueryWrapper();
            wrapper1.eq("id",comment.getUserId());
            List<User> users1 = userMapper.selectList(wrapper2);
            comment.setUserName(users1.get(0).getNickname());
        }
        PageInfo<Comment> commentPage = new PageInfo<>(comments);
        return commentPage;
    }

    @Override
    public void modifyStatus2(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setStatus(Integer.parseInt(status));
        commentMapper.updateById(comment);
    }
}
